<template>
  <el-form ref="activityRemarkRefForm" :model="activityRemarkQuery" label-width="110px" :rules="activityRemarkRules">

    <el-form-item label="ID：">
      <div class="activityItem">&nbsp;{{activityDetail.id}}</div>
    </el-form-item>

    <el-form-item label="负责人：">
      <div class="activityItem">&nbsp;{{activityDetail.ownerDO.name}}</div>
    </el-form-item>

    <el-form-item label="活动名称：">
      <div class="activityItem">&nbsp;{{activityDetail.name}}</div>
    </el-form-item>

    <el-form-item label="开始时间：">
      <div class="activityItem">&nbsp;{{activityDetail.startTime}}</div>
    </el-form-item>

    <el-form-item label="结束时间：">
      <div class="activityItem">&nbsp;{{activityDetail.endTime}}</div>
    </el-form-item>

    <el-form-item label="活动预算：">
      <div class="activityItem">&nbsp;{{activityDetail.cost}}</div>
    </el-form-item>

    <el-form-item label="活动描述：">
      <div class="activityItem">&nbsp;{{activityDetail.description}}</div>
    </el-form-item>

    <el-form-item label="创建时间：">
      <div class="activityItem">&nbsp;{{activityDetail.createTime}}</div>
    </el-form-item>

    <el-form-item label="创建人：">
      <div class="activityItem">&nbsp;{{activityDetail.createByDO.name}}</div>
    </el-form-item>

    <el-form-item label="编辑时间：">
      <div class="activityItem">&nbsp;{{activityDetail.editTime}}</div>
    </el-form-item>

    <el-form-item label="编辑人：">
      <div class="activityItem">&nbsp;{{activityDetail.editByDO.name}} </div>
    </el-form-item>

    <el-form-item label="填写备注：" prop="noteContent">
      <el-input
          v-model="activityRemarkQuery.noteContent"
          :rows="7"
          type="textarea"
          placeholder="请输入活动备注"/>
    </el-form-item>

    <el-form-item>
      <span class="dialog-footer">
        <el-button type="primary" @click="activityRemarkSubmit">提 交</el-button>
        <el-button @click="goBack">返 回</el-button>
      </span>
    </el-form-item>

    <el-form-item label="历史备注：">
      <el-table
          :data="activityRemarkList"
          style="width: 100%">
        <el-table-column type="index" label="序号" width="55"/>
        <el-table-column property="noteContent" label="备注内容"/>
        <el-table-column property="createTime" label="备注时间"/>
        <el-table-column property="createByDO.name" label="备注人"/>
        <el-table-column property="editTime" label="编辑时间"/>
        <el-table-column property="editByDO.name" label="编辑人"/>
        <el-table-column label="操作" show-overflow-tooltip>
          <template #default="scope"><!--插槽-->
            <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageSize"
          :total="total"
          @current-change="page"
          @prev-click="page"
          @next-click="page" />
    </el-form-item>
  </el-form>

  <!--编辑活动备注的弹窗（对话框）-->
  <el-dialog v-model="activityRemarkDialogVisible" title="编辑活动备注" width="55%" :center="true" :draggable="true">
    <el-form ref="editActivityRemarkRefForm" :model="editActivityRemarkQuery" label-width="110px" :rules="editActivityRemarkFormRules">

      <el-form-item label="活动备注：" prop="noteContent">
        <el-input
            v-model="editActivityRemarkQuery.noteContent"
            :rows="7"
            type="textarea"
            placeholder="请输入活动备注"/>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="editActivityRemarkSubmit">提 交</el-button>
        <el-button @click="activityRemarkDialogVisible = false">关 闭</el-button>
      </span>
    </template>
  </el-dialog>

</template>

<script>
import {doDelete, doGet, doPost, doPut} from "@/http/httpRequest";
import {messageConfirm, messageTip} from "@/util/util";

export default {
  name: "ActivityDetailView",

  //注入reload函数
  inject : ['reload'],

  data() {
    return {
      //定义市场活动详情对象，初始值是空
      activityDetail : {
        ownerDO : {},
        createByDO : {},
        editByDO : {}
      },
      //活动备注对象，初始值是空
      activityRemarkQuery : {},
      //活动备注的验证规则
      activityRemarkRules : {
        noteContent : [
          { required: true, message: '请输入活动备注', trigger: 'blur' },
          { min: 5, max: 255, message: '活动备注长度为5-255个字符', trigger: 'blur' },
        ]
      },
      //历史备注列表对象，初始值是空
      activityRemarkList : [{
        createByDO : {},
        editByDO: {}
      }],
      //分页时每页显示多少条数据，初始值是0
      pageSize : 0,
      //分页时总共有多少条数据，初始值是0
      total : 0,
      //编辑活动备注的弹窗是否弹出来，true就弹出来，false不弹出来
      activityRemarkDialogVisible : false,
      //要编辑的活动备注对象，初始值是空
      editActivityRemarkQuery : {},
      //编辑活动备注的验证规则
      editActivityRemarkFormRules : {
        noteContent : [
          { required: true, message: '请输入活动备注', trigger: 'blur' },
          { min: 5, max: 255, message: '活动备注长度为5-255个字符', trigger: 'blur' },
        ]
      }
    }
  },

  mounted() {
    this.loadActivityDetail();
    this.getActivityRemarkList();
  },

  methods : {
    //加载市场活动详情信息
    loadActivityDetail() {
      let id = this.$route.params.id; //params点后面的这个id名要和路由配置中的名称相同
      doGet("/api/activity/" + id, {}).then(resp => {
        if (resp.data.code === 200) {
          this.activityDetail = resp.data.data;
        }
      })
    },

    //提交市场活动备注
    activityRemarkSubmit() {
      this.$refs.activityRemarkRefForm.validate((isValid) => {
        if (isValid) {
          doPost("/api/activity/remark", {
            activityId : this.activityDetail.id,
            noteContent : this.activityRemarkQuery.noteContent
          }).then(resp => {
            if (resp.data.code === 200) {
              messageTip("提交成功", "success");
              //刷新一下，局部刷新
              this.reload();
            } else {
              messageTip("提交失败，原因：" + resp.data.msg, "error");
            }
          })
        }
      })
    },

    //获取市场活动备注列表数据（分页）
    getActivityRemarkList(current) {
      let id = this.$route.params.id; //params点后面的这个id名要和路由配置中的名称相同
      doGet("/api/activity/remark", {
        current : current, //前面的current是参数名，后面的current是参数值
        activityId : id
      }).then(resp => {
        if (resp.data.code === 200) {
          this.activityRemarkList = resp.data.data.list;
          this.pageSize = resp.data.data.pageSize;
          this.total = resp.data.data.total;
        }
      })
    },

    //分页函数
    page(value) { //value就是要查询的那一页的页码
      this.getActivityRemarkList(value);
    },

    //返回上一个路由页面
    goBack() {
      this.$router.go(-1);
    },

    //编辑活动备注
    edit(id) {
      console.log(id);
      this.activityRemarkDialogVisible = true;
      this.loadActivityRemarkDetail(id);
    },

    //编辑市场活动备注（提交保存）
    editActivityRemarkSubmit() {
      this.$refs.editActivityRemarkRefForm.validate( (isValid) => {
        if (isValid) {
          doPut("/api/activity/remark", {
            id : this.editActivityRemarkQuery.id,
            noteContent : this.editActivityRemarkQuery.noteContent
          }).then(resp => {
            if (resp.data.code === 200) {
              messageTip("编辑成功", "success");
              //刷新一下，局部刷新
              this.reload();
            } else {
              messageTip("编辑失败，原因：" + resp.data.msg, "error");
            }
          })
        }
      })
    },

    //根据id加载活动备注信息详情
    loadActivityRemarkDetail(id) {
      doGet("/api/activity/remark/" + id, {}).then(resp => {
        if (resp.data.code === 200) {
          this.editActivityRemarkQuery = resp.data.data;
        }
      })
    },

    del(id) {
      //提示用户，您确定要删除该数据吗？
      messageConfirm("您确定要删除该数据吗？").then(() => { //点“确定”就执行then()函数
        doDelete("/api/activity/remark/" + id, {}).then(resp => {
          //then里面取结果，这个结果对象可以使用任何名字，不一定要使用过resp，我们此处使用的就是ttt
          if (resp.data.code === 200) {
            messageTip("删除成功", "success");
            //刷新一下，局部刷新
            this.reload();
          } else {
            messageTip("删除失败，原因：" + resp.data.msg, "error");
          }
        })
      }).catch(() => { //点“取消”就执行catch()函数
        messageTip("取消删除", "warning");
      })
    }
  }
}
</script>

<style scoped>
.activityItem {
  background: aliceblue;
  width: 100%;
}
.el-pagination {
  margin-top: 10px;
}
</style>